using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class PlayerWeaponSpear : MonoBehaviour, IPlayerWeapon {
	static Vector3 THRUST_EXTENSION = new Vector3(0, 0, -1.8f);
	
	Vector3 local_pos;
	Quaternion local_rot;
	
	bool thrusting;
	PlayerToolAnimation thrust_ani;
	
	const bool DEBUG = true;
	
	void Start() {
		this.thrust_ani = new PlayerToolAnimation();
		this.thrust_ani.AddFrame(
			new PlayerToolAnimationFrame(
				0.3f,
				PlayerWeaponSpear.THRUST_EXTENSION,
				Quaternion.identity)
			);
		this.thrusting = false;
	}
	
	void Update() {
		if (this.thrust_ani.HasAnimationBegun()) {
			bool ended;
			PlayerToolAnimationFrame f;
			
			if (!this.thrusting) {
				this.local_rot = this.transform.localRotation;
				this.local_pos = this.transform.localPosition;
				this.thrusting = true;
			}
			
			f = this.thrust_ani.CheckNextFrame (Time.deltaTime, out ended);
			if (f != null) {
				this.transform.Translate(
					this.transform.localRotation * f.translation,
					Space.Self);
				this.transform.localRotation = this.transform.localRotation * f.rotation;
			}
			
			if (ended) {
				this.thrust_ani.EndAnimation ();
				this.thrusting = false;
				this.transform.localPosition = this.local_pos;
				this.transform.localRotation = this.local_rot;
			}
		}
	}
	
	public void BasicAttack() {			
		if ((this.thrust_ani != null) &&
			!this.thrust_ani.HasAnimationBegun()) {
			this.thrust_ani.BeginAnimation ();
		}
	}
	
	void OnTriggerStay(Collider c) {
		if (this.thrusting) {
			Debug.Log ("speared "  + c.ToString ());
		}
	}
}
